package com.xja.tt.controller;

import com.github.pagehelper.PageInfo;
import com.xja.tt.bean.Result;
import com.xja.tt.bean.Search;
import com.xja.tt.bean.Student;
import com.xja.tt.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * @author 李岩
 * @date 2021/12/22-20:26
 * @describe
 */
@Controller
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @RequestMapping("findAllStudent")
    @ResponseBody
    public Result findAllStudent(@RequestBody Search search){
        System.out.println("Search=   "+search);
        try {
            PageInfo<Student> pageInfo = studentService.findAllStudents(search);
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("stuList",pageInfo.getList());
            hashMap.put("pageNum",pageInfo.getPageNum());
            hashMap.put("total",pageInfo.getTotal());
            Result result = Result.getResult(true, 200, "展示所有成功", hashMap);
            System.out.println("结果  "+result);
            return result;
        } catch (Exception e) {
            System.out.println("查询出现异常   "+e);
        }
        Result result = Result.getResult(false, 444, "展示所有失败", null);
        System.out.println("结果  "+result);
        return result;
    }
    @RequestMapping("deleteStudent")
    @ResponseBody
    public Result deleteStudent(@RequestBody Search search){
        System.out.println("删除Search=   "+search);
        try {
            studentService.deleteStudent(search.getNumber());
            Result result = Result.getResult(true, 200, "删除成功", null);
            System.out.println("结果  "+result);
            return result;
        } catch (Exception e) {
            System.out.println("删除出现异常   "+e);
        }
        Result result = Result.getResult(false, 444, "展示所有失败", null);
        System.out.println("结果  "+result);
        return result;
    }

    //添加方法
    @RequestMapping("addStudent")
    @ResponseBody
    public Result addStudent(@RequestBody Student student){
        System.out.println(student.getNumber()+";"+student.getName());
        try {
            studentService.addstu(student);

            return Result.getResult(true,200,"添加成功",null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Result.getResult(false,200,"添加失败",null);
    }
    //回显方法
    @RequestMapping("findStudentByNumber")
    @ResponseBody
    public Result findStudentByNum(Integer number){
        try {
            studentService.finStudentByNumber(number);
            return Result.getResult(true,200,"回调成功",null);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return Result.getResult(false,400,"回调失败",null);
    }
    //修改方法
    @RequestMapping("updateStudent")
    @ResponseBody
    public Result updateStudent(@RequestBody Student student){
        System.out.println(student.getNumber()+";"+student.getName()+";"+student.getAddress());
        try {
            studentService.updateStudent(student);
            return Result.getResult(true,200,"修改成功",null);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return Result.getResult(false,400,"修改失败",null);
    }
}
